/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel;

import com.minecraft.moonlake.MoonLakeAPI;
import com.minecraft.moonlake.MoonLakePlugin;
import com.minecraft.moonlake.legendaryweaponrel.listeners.EntityDamageListener;
import com.minecraft.moonlake.legendaryweaponrel.listeners.PlayerListener;
import com.minecraft.moonlake.legendaryweaponrel.weapons.Weapon;
import com.minecraft.moonlake.legendaryweaponrel.weapons.WeaponCloseable;
import com.minecraft.moonlake.legendaryweaponrel.weapons.Weapons;
import com.minecraft.moonlake.util.StringUtil;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.logging.Level;
import java.util.stream.Stream;

public class LegendaryWeaponRelPlugin extends JavaPlugin {

    private String prefix;
    private static LegendaryWeaponRelPlugin MAIN;

    public LegendaryWeaponRelPlugin() {
    }

    @Override
    public void onEnable() {
        MAIN = this;

        if(!setupMoonLake()) {
            this.getLogger().log(Level.SEVERE, "前置月色之湖核心API插件加载失败.");
            this.getServer().getPluginManager().disablePlugin(this);
            return;
        }
        this.initFolder();

        MoonLakeAPI.registerEvent(new PlayerListener(this), this);
        MoonLakeAPI.registerEvent(new EntityDamageListener(this), this);

        this.getLogger().info("传奇武器重制 LegendaryWeaponRel 插件 v" + getDescription().getVersion() + " 成功加载.");
    }

    @Override
    public void onDisable() {
        // 处理传说武器的 close
        Stream.of(Weapons.values())
                // 筛选当前传说武器是否实现了武器close能力则进行关闭
                .filter(weapon -> weapon instanceof WeaponCloseable)
                .forEach(weapon -> ((WeaponCloseable) weapon).close());
    }

    public static LegendaryWeaponRelPlugin getInstance() {
        return MAIN;
    }

    private void initFolder() {
        if(!getDataFolder().exists())
            getDataFolder().mkdirs();
        File config = new File(getDataFolder(), "config.yml");
        if(!config.exists())
            saveDefaultConfig();
        reload();
    }

    private void reload() {
        this.prefix = StringUtil.toColor(getConfig().getString("Prefix", "&f[&d传奇武器&f] "));
    }

    public String getMessage(String key, Object... args) {
        return prefix + StringUtil.toColor(StringUtil.format(getConfig().getString("Messages." + key, ""), args));
    }

    private boolean setupMoonLake() {
        Plugin plugin = this.getServer().getPluginManager().getPlugin("MoonLake");
        return plugin != null && plugin instanceof MoonLakePlugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(!sender.hasPermission("moonlake.legendaryweapon.use")) {
            sender.sendMessage(getMessage("NotPermission"));
            return true;
        }
        Inventory inv = Bukkit.getServer().createInventory(null, 54, "LegendaryWeaponRel");
        for(final Weapon weapon : Weapons.values()) inv.addItem(weapon.build());
        ((Player) sender).openInventory(inv);
        return true;
    }
}
